package cn.bdqn.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bdqn.bean.Bill;
import cn.bdqn.bean.User;
import cn.bdqn.service.BillService;
import cn.bdqn.service.impl.BillServiceImpl;

public class BillAddServlet extends HttpServlet {

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//获取用户输入
		String billCode = request.getParameter("billId");
		String productName = request.getParameter("billName");
		String productUnit = request.getParameter("billCom");
		String billNum = request.getParameter("billNum");
		String money = request.getParameter("money");
		String supplier = request.getParameter("supplier");
		String zhifu = request.getParameter("zhifu");
		String productDesc = request.getParameter("productDesc");
		//处理数据
		Integer productAmount = null;
		Double totalPrice = null;
		Integer providerId = null;
		Integer isPay = null;
		if(billNum!=null && billNum!=""){
			productAmount = Integer.parseInt(billNum);
		}else{
			productAmount = 0;
		}
		
		if(money!=null && money!=""){
			totalPrice = Double.parseDouble(money);
		}
		if(supplier!=null && supplier!=""){
			providerId = Integer.parseInt(supplier);
		}
		if(zhifu!=null && zhifu!=""){
			isPay = Integer.parseInt(zhifu);
		}
		//创建bill对象
		Bill bill = new Bill(billCode, productName, productDesc, 
				productUnit, productAmount, totalPrice, isPay, providerId);
		//添加创建人信息
		User user = (User) request.getSession().getAttribute("user");
		bill.setCreateBy(user.getId());
		//调用后台添加方法
		BillService billService = new BillServiceImpl();
		if(billService.addBill(bill)){
			response.sendRedirect("BillListServlet");
		}else{
			response.sendRedirect("jsp/billAdd.jsp");
		}
		
	}

}
